package www.reagnetbox.com.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import www.reagnetbox.com.R;
import www.reagnetbox.com.adapter.QueryListAdapter;
import www.reagnetbox.com.bean.QueryListItem;
import www.reagnetbox.com.util.RecyclerViewUtil;
import www.reagnetbox.com.util.XPopupUtil;

public class QueryActivity extends AppCompatActivity implements View.OnClickListener {

    private List<QueryListItem> queryList = new ArrayList<>();
    private QueryListAdapter queryListAdapter;
    private Context context = this;
    private String[] operators = new String[]{"吴亦凡","蔡徐坤","易烊千玺","王源","王俊凯","肖战","王一博","郑爽"};
    private String[] companys = new String[]{"中国华润有限公司","中国医药集团","辉瑞制药有限公司","英国葛兰素史克公司","默沙东"};
    private SmartRefreshLayout refreshLayout;
    private TextView tvQueryIn;
    private TextView tvQueryOut;
    private TextView tvQueryWarning;
    private List<TextView> btns;
    private int page=1;
    private int queryType = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);
        ImmersionBar.with(this).init();
        initView();
        initData();
    }

    private void initView() {
        initTopButtonViews();
        initQueryListViews();
    }

    private void initTopButtonViews() {
        tvQueryIn = findViewById(R.id.tv_query_stock_in);
        tvQueryIn.setOnClickListener(this);
         tvQueryOut = findViewById(R.id.tv_query_stock_out);
        tvQueryOut.setOnClickListener(this);
        tvQueryWarning = findViewById(R.id.tv_query_warning);
        tvQueryWarning.setOnClickListener(this);
        findViewById(R.id.tv_back).setOnClickListener(this);
    }

    private void initQueryListViews() {
        refreshLayout = RecyclerViewUtil.initRefreshAndLoadMore(context, null, this::getQueryListData);
        queryListAdapter = new QueryListAdapter(this,R.layout.item_query_list, queryList);
        queryListAdapter.setOnItemClickListener((adapter, view, position) -> {
            QueryListItem data = queryList.get(position);
            String content = "编号："+data.getCode()+"\n"+
                    "操作员："+data.getOperator()+"\n"+
                    "名称："+data.getName()+"\n"+
                    "包装规格："+data.getModel()+"\n"+
                    "生产厂商："+data.getCompany()+"\n"+
                    "数量："+data.getNum()+"\n"+
                    "入库日期："+data.getStockInTime()+"\n"+
                    "失效日期："+data.getInvalidTime()+"\n"+
                    "位置编号："+data.getPlace();
            XPopupUtil.showKnowPopup(context,"记录详情",content);
        });
        RecyclerViewUtil.initRecyclerView(context, queryListAdapter);
    }

    private void initData() {
     getQueryListData();
    }

    private void refreshData(){
        page = 1;
        refreshLayout.setNoMoreData(false);
        getQueryListData();
    }

    private void getQueryListData() {
        for (int i = 0; i < 20; i++) {
            QueryListItem item = new QueryListItem();
            item.setCode("BMW002"+i);
            item.setOperator(operators[i%8]);
            item.setName("测试试剂名称"+i);
            item.setModel("100g/ml");
            item.setCompany(companys[i%5]);
            item.setNum(i*3);
            item.setStockInTime("2021-04-04");
            item.setInvalidTime("2021-05-05");
            item.setPlace("A0"+i);
            queryList.add(item);
        }
        queryListAdapter.notifyDataSetChanged();

//        Map<String, String> params=new HashMap<>();
//        params.put("pageSize","20");
//        params.put("pageNumber",page+"");
//        params.put("queryType",queryType+"");
//        NetUtil.post(context, "", params, response -> {
//
//        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.with(this).destroy();
    }

    public void setSelected(TextView tv){
        if (btns==null){
            btns = new ArrayList<>();
            btns.add(tvQueryIn);
            btns.add(tvQueryOut);
            btns.add(tvQueryWarning);
        }
        for (TextView btn : btns) {
            if (btn.equals(tv)){
                btn.setBackgroundResource(R.mipmap.bg_btn_selected);
            }else{
                btn.setBackgroundResource(R.mipmap.bg_btn_normal);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_query_stock_in:
                queryType = 1;
                setSelected(tvQueryIn);
                refreshData();
                break;
            case R.id.tv_query_stock_out:
                queryType = 2;
                setSelected(tvQueryOut);
                refreshData();
                break;
            case R.id.tv_query_warning:
                queryType = 3;
                setSelected(tvQueryWarning);
                refreshData();
                break;
            case R.id.tv_back:
                finish();
                break;
        }
    }
}
